<template>
	<view class="bg-light flex flex-column">
		<view class="bg-white px-3">
			<view class="py-3 border-bottom border-light flex align-center">
				<text class="txt font flex-shrink">收货人</text>
				<input v-model="form.Name" placeholder-class="font" placeholder="请输入收货人" style="width: 100%;" type="text">
			</view>
			<view class="py-3 flex align-center">
				<text class="txt font flex-shrink">手机号</text>
				<input maxlength="11" v-model="form.Phone" placeholder-class="font" placeholder="请输入手机号" style="width: 100%;" type="text">
			</view>
			<view class="py-3 border-bottom border-light flex align-center">
				<text class="txt font flex-shrink">地址</text>
				<view @click="getMapLocation" class="flex font justify-between align-center">
					<text>{{form.Address}}</text>
					<uni-icons type="bottom"></uni-icons>
				</view>
			</view>
			<view class="py-3 border-bottom border-light flex align-center">
				<text class="txt font flex-shrink">详情地址</text>
				<input v-model="form.Address_Details" placeholder-class="font" placeholder="街道门牌 楼层号等信息" style="width: 100%;" type="text">
			</view>
		</view>
		<!-- <view class="my-2 px-3 py-4 bg-white flex justify-between align-center">
			<text class="txt font">设置默认</text>
			<switch color="#828d96" style="transform:scale(0.8)" :checked="isDefault" @change="handleDefault()"/>
		</view> -->
		<view class="px-4 py-2 fixed-bottom ">
			<view class="bg-info rounded text-center py-2 text-white font-md"
				@click="handleBack()" hover-class="bg-hover-info" :hover-stay-time="150">保存并使用</view>
		</view>
	</view>
</template>

<script>
	import Util from '@/mixins/util.js'
	const app = getApp()
	export default {
		data() {
			return {
				Address:[],//省市区
				isDefault:false,//默认
				Path:null,//回调路径
				form:{
					Address_SerialNumber:'',
					Open_ID:app.globalData.openid,
					Address:'请选择收货地址',
					Address_Details:'',
					Name:'',
					Phone:'',
					latitude:"",
					longitude:'',
					Default_Type:'0',
					App_ID:app.globalData.appid
				}
			}
		},
		methods: {
			getMapLocation() {
				uni.chooseLocation({
					success: (res) => {
						let reg = /.+?(省|市|自治区|自治州|县|区)/g
						this.form.Address = res.address.match(reg)[0]+res.address.match(reg)[1]+res.address.match(reg)[2]
						this.form.Address_Details = res.address.replace(this.form.Address,'')
						this.form.latitude = res.latitude
						this.form.longitude = res.longitude
						console.log(res);
						// uni.setStorageSync('latitude', res.latitude)
						// uni.setStorageSync('longitude', res.longitude)
						// uni.setStorageSync('location',res.address)
					},
					fail: () => {
						// 如果用uni.chooseLocation没有获取到地理位置，则需要获取当前的授权信息，判断是否有地理授权信息
						uni.getSetting({
							success: (res) => {
								console.log(res);
								var status = res.authSetting;
								if (!status['scope.userLocation']) {
									// 如果授权信息中没有地理位置的授权，则需要弹窗提示用户需要授权地理信息
									uni.showModal({
										title: "是否授权当前位置",
										content: "需要获取您的地理位置，请确认授权，否则地图功能将无法使用",
										success: (tip) => {
											if (tip.confirm) {
												// 如果用户同意授权地理信息，则打开授权设置页面，判断用户的操作
												uni.openSetting({
													success: (data) => {
														// 如果用户授权了地理信息在，则提示授权成功
														if (data
															.authSetting[
																'scope.userLocation'
															] === true
														) {
															uni.showToast({
																title: "授权成功",
																icon: "success",
																duration: 1000
															})
															// 授权成功后，然后再次chooseLocation获取信息
															uni.chooseLocation({
																success: (
																	res
																) => {
																	console
																		.log(
																			"详细地址",
																			res
																		);
																}
															})
														} else {
															uni.showToast({
																title: "授权失败",
																icon: "none",
																duration: 1000
															})
														}
													}
												})
											}
										}
									})
								}
							},
							fail: (res) => {
								uni.showToast({
									title: "调用授权窗口失败",
									icon: "none",
									duration: 1000
								})
							}
						})
					}
				});
			},
			bindAdressChange(e){	//地区选择器
				this.Address = e.detail.value;
				this.form.Address = this.Address[0]+"/"+this.Address[1]+"/"+this.Address[2]
			},
			handleDefault(){	//设置默认
				this.isDefault = !this.isDefault
				if(this.isDefault){this.form.Default='1'}else{this.form.Default='0'}
			},
			handleBack(){	//提交回退
				if(Util.validateContacts(this.form.Name)&&Util.validatemobile(this.form.Phone)&&Util.validateadress(this.form.Address)&&Util.validateadress(this.form.Address_Details)){
					uni.request({
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						url: app.globalData.Domain_Name + '/WXAddAction', //添加地址
						method: "POST",
						data: { Data:JSON.stringify({member_address_info:[this.form]}),Open_ID:app.globalData.openid},
						success: (res) => {
							if(res.data.Status=='success'){
								uni.showToast({ title: '提交成功！',icon:'success'})
								const timer = setTimeout(() => {
									clearTimeout(timer)
									uni.navigateBack()//后退
								}, 500)
							}
						}
					})
				}	
			}
		}	
	}
</script>

<style>
.txt{
	display: inline-block;
	width: 25%;
}
</style>
